package leetcode.ge100;

import leetcode.lt100.TreeNode;

import java.util.ArrayList;
import java.util.List;


class Solution_113 {


    public static void main(String[] args) {
    }

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        dfs(res, temp, root, sum);
        return res;
    }

    private void dfs(List<List<Integer>> res, List<Integer> temp, TreeNode root, int sum) {
        if (root == null) {
            return;
        }
        temp.add(root.val);
        if (root.left == null && root.right == null && root.val == sum) {
            res.add(new ArrayList<>(temp));
        } else {
            dfs(res, temp, root.left, sum - root.val);
            dfs(res, temp, root.right, sum - root.val);
        }
        temp.remove(temp.size() - 1);
    }


    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        if (root.left == null && root.right == null) {
            return sum == root.val;
        }
        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    }


}